package _21_多线程juc.demo01;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 方法四
 * 1. java 中的线程池是 Executors 框架
 *
 * 2. ThreadPoolExecutor 类是线程池创建的核心类，有 7 个参数可设置，5 种拒绝策略
 * 3. Executors 提供了 5 种常用的线程池，应用场景
 */
public class MyThread4 {

    // 使用线程池 newFixedThreadPool
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 5; i++) {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        pool.shutdown();
    }

}
